package com.gaff.emp.core.modules.perm.controller.role;

import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.R;
import com.gaff.emp.core.modules.auth.annotation.Auth;
import com.gaff.emp.core.modules.perm.controller.role.form.DeleteForm;
import com.gaff.emp.core.modules.perm.controller.role.form.CreateForm;
import com.gaff.emp.core.modules.perm.controller.role.form.UpdateForm;
import com.gaff.emp.core.modules.perm.entity.SysMenu;
import com.gaff.emp.core.modules.perm.entity.SysRole;
import com.gaff.emp.core.modules.perm.entity.SysRoleMenu;
import com.gaff.emp.core.modules.perm.service.SysRoleService;
import com.gaff.emp.core.syslog.annotation.SysOperaLog;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation("分页获取角色列表")
    @SysOperaLog(value = "分页获取角色列表", isPersistence = false)
    @Auth(perm = "role")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok(sysRoleService.queryRolesByPage(params));
    }


    @GetMapping("/all")
    @ApiOperation("获取全部角色列表")
    @SysOperaLog(value = "获取全部角色列表", isPersistence = false)
    @Auth(perm = "role")
    public R all() {
        return R.ok(sysRoleService.queryAllRoles());
    }


    @PostMapping("/create")
    @ApiOperation("新增角色")
    @SysOperaLog(value = "新增角色")
    @Auth(perm = "role")
    public R create(@RequestBody @Valid CreateForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysRoleService.createRole(form);
        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("更新角色")
    @SysOperaLog(value = "更新角色")
    @Auth(perm = "role")
    public R update(@RequestBody @Valid UpdateForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysRoleService.updateRole(form);
        return R.ok();
    }

    @GetMapping("/info/{id}")
    @ApiOperation("获取角色信息")
    @SysOperaLog(value = "获取角色信息", isPersistence = false)
    @Auth(perm = "role")
    public R info(@PathVariable("id") Integer id) {
        SysRole role = sysRoleService.queryById(id);
        if (role == null) {
            throw new EmpException("角色不存在");
        }
        List<SysMenu> menus = sysRoleService.queryMenusByRoleId(id);
        Map<String, Object> result = new HashMap<>();
        result.put("role", role);
        result.put("menus", menus);
        return R.ok(result);
    }

    @PostMapping("/delete")
    @ApiOperation("删除角色")
    @SysOperaLog(value = "删除角色")
    @Auth(perm = "role")
    public R delete(@RequestBody @Valid DeleteForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysRoleService.deleteById(form.getId());
        return R.ok();
    }


}
